import random
import math
import json

class DroneController:
    def __init__(self):
        self.id_head = "identity_"
        self.dats = []
        self.count = 0

    def solve(self, sli):
        if len(sli) <= 1:
            return [], ValueError("参数不合法")
        
        for i in range(len(sli)-1, 0, -1):
            rand_num = random.randint(0, i)
            sli[i], sli[rand_num] = sli[rand_num], sli[i]
        return sli

    def get_move_up(self, num_steps):
        dat = {"id": self.id_head + str(self.count), "conversations": []}
        for i in range(0, num_steps, 2):
            dat["conversations"].append({"from": "user", "value": f"向上移动{i}个单位"})
            dat["conversations"].append({"from": "assistant", "value": f"aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1], aw.get_drone_position()[2]-{i}])"})
            self.count += 1
        self.dats.append(dat)

    def get_move_down(self, num_steps):
        dat = {"id": self.id_head + str(self.count), "conversations": []}
        for i in range(0, num_steps, 2):
            dat["conversations"].append({"from": "user", "value": f"向下移动{i}个单位"})
            dat["conversations"].append({"from": "assistant", "value": f"aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1], aw.get_drone_position()[2]+{i}])"})
            self.count += 1
        self.dats.append(dat)

    def get_move_w(self, num_steps):
        dat = {"id": self.id_head + str(self.count), "conversations": []}
        for i in range(0, num_steps, 2):
            dat["conversations"].append({"from": "user", "value": f"向前移动{i}个单位"})
            dat["conversations"].append({"from": "assistant", "value": f"aw.fly_to([aw.get_drone_position()[0]+{i}, aw.get_drone_position()[1], aw.get_drone_position()[2]])"})
            self.count += 1
        self.dats.append(dat)

    def get_move_s(self, num_steps):
        dat = {"id": self.id_head + str(self.count), "conversations": []}
        for i in range(0, num_steps, 2):
            dat["conversations"].append({"from": "user", "value": f"向后移动{i}个单位"})
            dat["conversations"].append({"from": "assistant", "value": f"aw.fly_to([aw.get_drone_position()[0]-{i}, aw.get_drone_position()[1], aw.get_drone_position()[2]])"})
            self.count += 1
        self.dats.append(dat)

    def get_move_right(self, num_steps):
        dat = {"id": self.id_head + str(self.count), "conversations": []}
        for i in range(0, num_steps, 2):
            dat["conversations"].append({"from": "user", "value": f"向右移动{i}个单位"})
            dat["conversations"].append({"from": "assistant", "value": f"aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1]+{i}, aw.get_drone_position()[2]])"})
            self.count += 1
        self.dats.append(dat)

    def get_move_left(self, num_steps):
        dat = {"id": self.id_head + str(self.count), "conversations": []}
        for i in range(0, num_steps, 2):
            dat["conversations"].append({"from": "user", "value": f"向左移动{i}个单位"})
            dat["conversations"].append({"from": "assistant", "value": f"aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1]-{i}, aw.get_drone_position()[2]])"})
            self.count += 1
        self.dats.append(dat)

    def get_move_take_off(self):
        take_off_phrases = ["无人机起飞", "无人机起行", "无人机起来", "起飞无人机", "无人机启动"]
        for phrase in take_off_phrases:
            dat = {"id": self.id_head + str(self.count), "conversations": []}
            dat["conversations"].append({"from": "user", "value": phrase})
            dat["conversations"].append({"from": "assistant", "value": "```python aw.takeoff()```"})
            self.dats.append(dat)
            self.count += 1

    def get_move_land(self):
        land_phrases = ["无人机降落", "无人机着陆", "无人机不飞了", "降落无人机", "无人机停止飞行", "无人机别飞了", "着陆无人机"]
        for phrase in land_phrases:
            dat = {"id": self.id_head + str(self.count), "conversations": []}
            dat["conversations"].append({"from": "user", "value": phrase})
            dat["conversations"].append({"from": "assistant", "value": "```python aw.land()```"})
            self.dats.append(dat)
            self.count += 1

    def get_move_got_zero(self):
        go_home_phrases = ["无人机回家", "无人机返回原点", "返回原点无人机", "无人机回到原点", "无人机返回初始位置", "返回原点", "回到原点", "飞往原点", "回到原点无人机", "飞往原点无人机"]
        for phrase in go_home_phrases:
            dat = {"id": self.id_head + str(self.count), "conversations": []}
            dat["conversations"].append({"from": "user", "value": phrase})
            dat["conversations"].append({"from": "assistant", "value": "```python aw.fly_to([0, 0, 0])```"})
            self.dats.append(dat)
            self.count += 1

    def get_move_rate_right(self, max_angle, max_distance):
        for i in range(0, max_angle, 5):
            angle = i + random.random()
            distance = i + random.random() * 10
            if i % 2 == 0:
                user_input = f"向右前方{angle:.2f}度移动{int(distance)}个单位"
            else:
                user_input = f"右前方{angle:.2f}度移{int(distance)}"
            
            dat = {"id": self.id_head + str(self.count), "conversations": []}
            dat["conversations"].append({"from": "user", "value": user_input})
            
            python_code = f"""
import math

#计算新的位置坐标，无人机向前{int(distance)}米，偏航角度{angle:.2f}度，向右{int(distance)}米，偏航角度{angle:.2f}度
delta_x={int(distance)}*math.cos(math.radians({angle:.2f}))
delta_y={int(distance)}*math.sin(math.radians({angle:.2f}))

new_x=aw.get_drone_position()[0]+delta_x
new_y=aw.get_drone_position()[1]+delta_y
new_z=aw.get_drone_position()[2]

#飞往新的位置坐标
aw.fly_to([new_x,new_y,new_z])
"""
            dat["conversations"].append({"from": "assistant", "value": f"```python \n{python_code}\n```"})
            self.dats.append(dat)
            self.count += 1

    def get_move_rate_left(self, max_angle, max_distance):
        for i in range(0, max_angle, 3):
            angle = i + random.random()
            distance = i + random.random() * 10
            if i % 2 == 0:
                user_input = f"向左前方{angle:.2f}度移动{int(distance)}个单位" 
            else:
                user_input = f"左前方{angle:.2f}度移{int(distance)}"
            
            dat = {"id": self.id_head + str(self.count), "conversations": []}
            dat["conversations"].append({"from": "user", "value": user_input})
            
            python_code = f"""
import math

#计算新的位置坐标，无人机向前{int(distance)}米，偏航角度{angle:.2f}度，向左{int(distance)}米，偏航角度{angle:.2f}度 
delta_x={int(distance)}*math.cos(math.radians({angle:.2f}))
delta_y={int(distance)}*math.sin(math.radians({angle:.2f}))

new_x=aw.get_drone_position()[0]+delta_x
new_y=aw.get_drone_position()[1]-delta_y
new_z=aw.get_drone_position()[2]

#飞往新的位置坐标
aw.fly_to([new_x,new_y,new_z])
"""
            dat["conversations"].append({"from": "assistant", "value": f"```python \n{python_code}\n```"})
            self.dats.append(dat)
            self.count += 1

    def run(self):
        self.get_move_up(100)
        self.get_move_down(100)
        self.get_move_w(100)
        self.get_move_s(100)
        self.get_move_right(100)
        self.get_move_left(100)
        self.get_move_take_off()
        self.get_move_land()
        self.get_move_got_zero()
        self.get_move_rate_right(360, 360)
        self.get_move_rate_left(360, 360)

        self.dats = self.solve(self.dats)
        for i in range(len(self.dats)):
            self.dats[i]["id"] = self.id_head + str(i)

        print(len(self.dats))
        with open("llms/data.json", "w", encoding='utf-8') as f:
            json.dump(self.dats, f, ensure_ascii=False, indent=4)

if __name__ == "__main__":
    controller = DroneController()
    controller.run()
